package com.company.笔试.快排;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;


/**
 * @author ylr
 * @version 1.0.0
 * @ClassName Queue.java
 * @Description TODO
 * @createTime 2022年02月24日 14:49:00
 */

class QueueNode {
    private QueueNode next;
    private int val;

    public QueueNode(int val) {
        this.val = val;
    }

    public QueueNode getNext() {
        return next;
    }

    public void setNext(QueueNode next) {
        this.next = next;
    }

    public int getVal() {
        return val;
    }

    public void setVal(int val) {
        this.val = val;
    }
}


public class Queue {
    private static ReentrantLock lock = new ReentrantLock();
    private long timeout = 0L;
    private  int size = 0;
    private  int cap = 0;
    private  QueueNode top;
    private  QueueNode tail;
    public static volatile String totalLog;
    private String tmpLog;


    public static String getTotalLog() {
        return totalLog;
    }

    public static synchronized void setTotalLog(String totalLog) {
        Queue.totalLog =Queue.totalLog+" \n "+ totalLog;
    }


    public Queue(int cap) {
        this.cap = cap;
    }





    public Boolean add(int val, long timeout) throws InterruptedException {
        if (lock.tryLock(timeout, TimeUnit.MILLISECONDS)) {
            while (lock.isLocked()){//如果被锁住,重试
                if (size <cap) {
                    if (top == null) {
                        top = new QueueNode(val);
                        tail=top;
                    }else {
                        tail.setNext(new QueueNode(val));
                        tail= tail.getNext();
                    }
                    setTmpLog("插入>>>>>>>>>>  "+val);
                    size++;
                    lock.unlock();
                    setTotalLog(getTmpLog());
                    return true;
                }
                System.out.println("容量不够！！");
            }
            setTmpLog("---add------超时解锁------------");
            lock.unlock();
            setTotalLog(getTmpLog());
        }
        return false;
    }

    public Integer pop(long timeout) throws InterruptedException {
        if (lock.tryLock(timeout, TimeUnit.MILLISECONDS)) {
            while (lock.isLocked()){//如果被锁住,重试
                if (size >0) {
                    Integer res=null;
                    if (top == null) {
                        return res;
                    }
                    res = top.getVal();
                    top=top.getNext();
                    size--;
                    setTmpLog("取出>>>>>>>>>>  "+res);
                    lock.unlock();
                    setTotalLog(getTmpLog());
                    return res;
                }
                System.out.println("无元素！！");

            }
            setTmpLog("----pop-----超时解锁------------");
            lock.unlock();
            setTotalLog(getTmpLog());
        }
        return null;
    }

    public String getTmpLog() {
        return tmpLog;
    }

    public void setTmpLog(String tmpLog) {
        this.tmpLog = tmpLog;
    }
}
